package com.agger.harmonyosthemeseditor.servicescard;

import com.agger.harmonyosthemeseditor.ResourceTable;
import com.agger.harmonyosthemeseditor.intelligent.DatabaseManager;
import com.agger.harmonyosthemeseditor.userinterface.ResourceTool;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.FormBindingData;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Switch;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.utils.zson.ZSONObject;

public class CardManagerAbility extends Ability
{
    int BgMode = 0;

    @Override
    public void onStart(Intent intent)
    {
        long cardId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY,-1);
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_widget_normal_editor);
        this.getWindow().setStatusBarColor(ResourceTool.getColor(this,ResourceTable.Color_dawnlighti_statusbar_normal,0xF7EDD8));
        this.getWindow().setNavigationBarColor(ResourceTool.getColor(this,ResourceTable.Color_dawnlighti_navigationbar_normal,0xF7EDD8));
        int CurrentColorMode = getColorMode();
        if(CurrentColorMode == 0) this.getWindow().setStatusBarVisibility(0);
        else if(CurrentColorMode == 1) this.getWindow().setStatusBarVisibility(2052);
        this.getWindow().setBackgroundColor(RgbColor.fromArgbInt(ResourceTool.getColor(this,ResourceTable.Color_dawnlighti_statusbar_normal,0xFEEFF4)));
        DisplayAttributes displayAttributes = DisplayManager.getInstance().getDefaultDisplay(this).get().getAttributes();

        Image bg_method01 = (Image)findComponentById(ResourceTable.Id_bg_01);
        Image bg_method02 = (Image)findComponentById(ResourceTable.Id_bg_02);
        Image bg_method03 = (Image)findComponentById(ResourceTable.Id_bg_03);
        Image bg_method04 = (Image)findComponentById(ResourceTable.Id_bg_04);
        Image bg_method05 = (Image)findComponentById(ResourceTable.Id_bg_05);
        Image bg_method06 = (Image)findComponentById(ResourceTable.Id_bg_06);
        Component bg_border01 = findComponentById(ResourceTable.Id_bg_border01);
        Component bg_border02 = findComponentById(ResourceTable.Id_bg_border02);
        Component bg_border03 = findComponentById(ResourceTable.Id_bg_border03);
        Component bg_border04 = findComponentById(ResourceTable.Id_bg_border04);
        Component bg_border05 = findComponentById(ResourceTable.Id_bg_border05);
        Component bg_border06 = findComponentById(ResourceTable.Id_bg_border06);

        bg_method01.setCornerRadius(9f*displayAttributes.scalDensity);
        bg_method02.setCornerRadius(9f*displayAttributes.scalDensity);
        bg_method03.setCornerRadius(9f*displayAttributes.scalDensity);
        bg_method04.setCornerRadius(9f*displayAttributes.scalDensity);
        bg_method05.setCornerRadius(9f*displayAttributes.scalDensity);
        bg_method06.setCornerRadius(9f*displayAttributes.scalDensity);

        DatabaseManager.Init(this);
        BgMode = DatabaseManager.getInt("FormBgIndex_"+cardId,0);

        Switch Switch_TextEntrance = (Switch)findComponentById(ResourceTable.Id_Settings_TextEntrance);
        Switch_TextEntrance.setChecked(DatabaseManager.getInt("FormTextEntrance_"+cardId,0)==1);
        Switch_TextEntrance.setCheckedStateChangedListener((button,isChecked)->
        {
            ZSONObject zsonObject = new ZSONObject();
            zsonObject.put("textEntrance", isChecked);
            FormBindingData formBindingData = new FormBindingData(zsonObject);
            try
            {
                DatabaseManager.Init(getContext());
                updateForm(cardId, formBindingData);
                DatabaseManager.setInt("FormTextEntrance_"+cardId,isChecked?1:0);
            } catch (Exception e) {e.printStackTrace();}
            DatabaseManager.Init(getContext());
        });

        if(BgMode==0)
        {
            bg_border01.setVisibility(Component.VISIBLE);
            bg_border02.setVisibility(Component.INVISIBLE);
            bg_border03.setVisibility(Component.INVISIBLE);
            bg_border04.setVisibility(Component.INVISIBLE);
            bg_border05.setVisibility(Component.INVISIBLE);
            bg_border06.setVisibility(Component.INVISIBLE);
        }else if(BgMode==1)
        {
            bg_border01.setVisibility(Component.INVISIBLE);
            bg_border02.setVisibility(Component.VISIBLE);
            bg_border03.setVisibility(Component.INVISIBLE);
            bg_border04.setVisibility(Component.INVISIBLE);
            bg_border05.setVisibility(Component.INVISIBLE);
            bg_border06.setVisibility(Component.INVISIBLE);
        }else if(BgMode==2)
        {
            bg_border01.setVisibility(Component.INVISIBLE);
            bg_border02.setVisibility(Component.INVISIBLE);
            bg_border03.setVisibility(Component.VISIBLE);
            bg_border04.setVisibility(Component.INVISIBLE);
            bg_border05.setVisibility(Component.INVISIBLE);
            bg_border06.setVisibility(Component.INVISIBLE);
        }else if(BgMode==3)
        {
            bg_border01.setVisibility(Component.INVISIBLE);
            bg_border02.setVisibility(Component.INVISIBLE);
            bg_border03.setVisibility(Component.INVISIBLE);
            bg_border04.setVisibility(Component.VISIBLE);
            bg_border05.setVisibility(Component.INVISIBLE);
            bg_border06.setVisibility(Component.INVISIBLE);
        }else if(BgMode==4)
        {
            bg_border01.setVisibility(Component.INVISIBLE);
            bg_border02.setVisibility(Component.INVISIBLE);
            bg_border03.setVisibility(Component.INVISIBLE);
            bg_border04.setVisibility(Component.INVISIBLE);
            bg_border05.setVisibility(Component.VISIBLE);
            bg_border06.setVisibility(Component.INVISIBLE);
        }else if(BgMode==5)
        {
            bg_border01.setVisibility(Component.INVISIBLE);
            bg_border02.setVisibility(Component.INVISIBLE);
            bg_border03.setVisibility(Component.INVISIBLE);
            bg_border04.setVisibility(Component.INVISIBLE);
            bg_border05.setVisibility(Component.INVISIBLE);
            bg_border06.setVisibility(Component.VISIBLE);
        }else
        {
            BgMode = 0;
            bg_border01.setVisibility(Component.VISIBLE);
            bg_border02.setVisibility(Component.INVISIBLE);
            bg_border03.setVisibility(Component.INVISIBLE);
            bg_border04.setVisibility(Component.INVISIBLE);
            bg_border05.setVisibility(Component.INVISIBLE);
            bg_border06.setVisibility(Component.INVISIBLE);
        }

        bg_method01.setClickedListener(c->
        {
            ZSONObject zsonObject = new ZSONObject();
            zsonObject.put("backgroundImage", "/common/background_0.png");
            zsonObject.put("isReady", true);
            FormBindingData formBindingData = new FormBindingData(zsonObject);
            try
            {
                DatabaseManager.Init(getContext());
                updateForm(cardId, formBindingData);
                DatabaseManager.setInt("FormBgIndex_"+cardId,0);
                BgMode = 0;
                bg_border02.setVisibility(Component.INVISIBLE);
                bg_border03.setVisibility(Component.INVISIBLE);
                bg_border04.setVisibility(Component.INVISIBLE);
                bg_border05.setVisibility(Component.INVISIBLE);
                bg_border06.setVisibility(Component.INVISIBLE);
                bg_border01.setVisibility(Component.VISIBLE);
            } catch (Exception e) {e.printStackTrace();}
            terminateAbility();
        });
        bg_method02.setClickedListener(c->
        {
            ZSONObject zsonObject = new ZSONObject();
            zsonObject.put("backgroundImage", "/common/background_1.png");
            zsonObject.put("isReady", true);
            FormBindingData formBindingData = new FormBindingData(zsonObject);
            try
            {
                DatabaseManager.Init(getContext());
                updateForm(cardId, formBindingData);
                DatabaseManager.setInt("FormBgIndex_"+cardId,1);
                BgMode = 1;
                bg_border01.setVisibility(Component.INVISIBLE);
                bg_border03.setVisibility(Component.INVISIBLE);
                bg_border04.setVisibility(Component.INVISIBLE);
                bg_border05.setVisibility(Component.INVISIBLE);
                bg_border06.setVisibility(Component.INVISIBLE);
                bg_border02.setVisibility(Component.VISIBLE);
            } catch (Exception e) {e.printStackTrace();}
            terminateAbility();
        });
        bg_method03.setClickedListener(c->
        {
            ZSONObject zsonObject = new ZSONObject();
            zsonObject.put("backgroundImage", "/common/background_2.png");
            zsonObject.put("isReady", true);
            FormBindingData formBindingData = new FormBindingData(zsonObject);
            try
            {
                DatabaseManager.Init(getContext());
                updateForm(cardId, formBindingData);
                DatabaseManager.setInt("FormBgIndex_"+cardId,2);
                BgMode = 2;
                bg_border01.setVisibility(Component.INVISIBLE);
                bg_border02.setVisibility(Component.INVISIBLE);
                bg_border04.setVisibility(Component.INVISIBLE);
                bg_border05.setVisibility(Component.INVISIBLE);
                bg_border06.setVisibility(Component.INVISIBLE);
                bg_border03.setVisibility(Component.VISIBLE);
            } catch (Exception e) {e.printStackTrace();}
            terminateAbility();
        });
        bg_method04.setClickedListener(c->
        {
            ZSONObject zsonObject = new ZSONObject();
            zsonObject.put("backgroundImage", "/common/background_3.png");
            zsonObject.put("isReady", true);
            FormBindingData formBindingData = new FormBindingData(zsonObject);
            try
            {
                DatabaseManager.Init(getContext());
                updateForm(cardId, formBindingData);
                DatabaseManager.setInt("FormBgIndex_"+cardId,3);
                BgMode = 3;
                bg_border01.setVisibility(Component.INVISIBLE);
                bg_border02.setVisibility(Component.INVISIBLE);
                bg_border03.setVisibility(Component.INVISIBLE);
                bg_border05.setVisibility(Component.INVISIBLE);
                bg_border06.setVisibility(Component.INVISIBLE);
                bg_border04.setVisibility(Component.VISIBLE);
            } catch (Exception e) {e.printStackTrace();}
            terminateAbility();
        });
        bg_method05.setClickedListener(c->
        {
            ZSONObject zsonObject = new ZSONObject();
            zsonObject.put("backgroundImage", "/common/background_4.png");
            zsonObject.put("isReady", true);
            FormBindingData formBindingData = new FormBindingData(zsonObject);
            try
            {
                DatabaseManager.Init(getContext());
                updateForm(cardId, formBindingData);
                DatabaseManager.setInt("FormBgIndex_"+cardId,4);
                BgMode = 4;
                bg_border01.setVisibility(Component.INVISIBLE);
                bg_border02.setVisibility(Component.INVISIBLE);
                bg_border03.setVisibility(Component.INVISIBLE);
                bg_border04.setVisibility(Component.INVISIBLE);
                bg_border06.setVisibility(Component.INVISIBLE);
                bg_border05.setVisibility(Component.VISIBLE);
            } catch (Exception e) {e.printStackTrace();}
            terminateAbility();
        });
        bg_method06.setClickedListener(c->
        {
            ZSONObject zsonObject = new ZSONObject();
            zsonObject.put("backgroundImage", "/common/background_5.png");
            zsonObject.put("isReady", true);
            FormBindingData formBindingData = new FormBindingData(zsonObject);
            try
            {
                DatabaseManager.Init(getContext());
                updateForm(cardId, formBindingData);
                DatabaseManager.setInt("FormBgIndex_"+cardId,5);
                BgMode = 5;
                bg_border01.setVisibility(Component.INVISIBLE);
                bg_border02.setVisibility(Component.INVISIBLE);
                bg_border03.setVisibility(Component.INVISIBLE);
                bg_border04.setVisibility(Component.INVISIBLE);
                bg_border05.setVisibility(Component.INVISIBLE);
                bg_border06.setVisibility(Component.VISIBLE);
            } catch (Exception e) {e.printStackTrace();}
            terminateAbility();
        });
        findComponentById(ResourceTable.Id_actionBar_Back).setClickedListener(c->onBackPressed());
    }

    @Override
    public void onBackPressed()
    {
        terminateAbility();
    }

    @Override
    public void onBackground()
    {
        terminateAbility();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

}
